using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.Helper;
using Ent.Voice.Nomination;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Skinner;
using Ent.Voice.Show;

public partial class UpdateEntry : PageSkinner, IPageWizard
{
    public string GetTitle(Dictionary<string, object> values)
    {
        return (values != null && values.ContainsKey(NominationProperty.Title) ? values[NominationProperty.Title].ToString() : "Update nomination");
    }
    public string SuccessCode { get { return  null; } }
    public string ErrorCode { get { return "ErrorEntryUpdation"; } }
    public string Operation 
    { 
        get
        {
            return "Update Entry";
        } 
    }
    public bool CanProcessWizard
    {
        get
        {
            try
            {
                int id = MathFunction.ConvertToInteger(Request.QueryString["id"]);
                Entry en = Entry.GetEntry(id, "");
                Skin_wizard wiz = ((Skin_wizard)this.Master);
                if (en.Nominee.UserID.ToLower() != Session["USERID"].ToString().ToLower())
                {
                    wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Ent_AccessDenied")));
                    return false;
                }
                wiz.Values.Add(NominationProperty.EntryId, id);
                wiz.Values.Add(NominationProperty.Title, en.Title);
                wiz.Values.Add(NominationProperty.Description, en.Description);
                return true;
            }
            catch (Exception exc)
            {
                Common.ErrorPage(exc);
            }
            return false;
        }
    }
    public void SetWizard(Ent.Web.WebControl.UserControl c)
    {

    }
    public bool ProcessWizardData(Dictionary<string, object> values)
    {
        try
        {
            Nomination nData = new Nomination();
            nData.EntryId = Convert.ToInt32(values[NominationProperty.EntryId]);
            nData.Title = (values.ContainsKey(NominationProperty.Title) ? values[NominationProperty.Title].ToString() : "");
            nData.Description = (values.ContainsKey(NominationProperty.Description) ? values[NominationProperty.Description].ToString() : "");
            nData.Invitees = values.ContainsKey(EventProperty.Invitees) ? values[EventProperty.Invitees].ToString() : "";

            NominationResponse res = Nomination.Update(nData);
            if (res == NominationResponse.NominationUpdated)
            {
                Ent.Web.Mail.Processor p;
                Ent.Web.Mail.MailContent cont;
                string[] emails = nData.Invitees.Split(new char[] { ',', ';', ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string email in emails)
                {
                    if (Ent.Web.Mail.Processor.IsValidEmail(email))
                    {
                        p = new Ent.Web.Mail.Processor();
                        cont = Ent.Web.Mail.Collection.GetMailContent("InviteVoters");
                        p.To = email;
                        p.Subject = string.Format(cont.Subject, nData.Title);
                        p.Body = string.Format(cont.Body, Session["FNAME"].ToString(), nData.Title, ConfigurationManager.AppSettings.Get("BaseUrl") + "/ShowEntry.aspx?id=" + nData.EntryId.ToString());
                        p.SendMail();
                    }
                }
                Response.Redirect("ShowEntry.aspx?id=" + nData.EntryId.ToString(), false);
                return true;
            }
            else
                return false;
        }
        catch (Exception exc)
        {
            Common.Log.Error(exc);
            return false;
        }
        return false;
    }
}
